package com.macro.mall.service.impl;

import com.google.common.collect.Lists;
import com.macro.mall.dto.CaseDto;
import com.macro.mall.dto.CasePicDto;
import com.macro.mall.mapper.CaseMapper;
import com.macro.mall.model.CaseEntity;
import com.macro.mall.model.CasePicEntity;
import com.macro.mall.model.caseStyleEntity;
import com.macro.mall.service.CaseService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  案例管理接口
 *
 * @author zhang
 * @date 2021/06/19
 */
@Service
public class CaseServiceImpl implements CaseService {
    @Autowired
    private CaseMapper caseMapper;

    @Override
    public List<CaseEntity> findCaseList(Long tenantId, Integer pageSize, Integer curPage) {
        Integer skip = (curPage-1)*pageSize;
        return caseMapper.findCaseList(tenantId, pageSize, skip);
    }

    @Override
    public int findCaseCount(Long tenantId) {
        return caseMapper.findCaseCount(tenantId);
    }

    @Override
    public CasePicEntity findCaseInfo(Long caseId) {
        CaseEntity caseInfo = caseMapper.findCaseInfo(caseId);
        if(caseInfo == null){
            return new CasePicEntity();
        }
        List<CasePicDto> casePicList = caseMapper.findCasePicList(caseId);
        CasePicEntity entity = new CasePicEntity();
        BeanUtils.copyProperties(caseInfo, entity);

        if(CollectionUtils.isNotEmpty(casePicList)){
            //图片类型（1banner2详情）
            List<String> bannerPicsList = casePicList.stream()
                    .filter(doc->doc.getPicType() == 1).map(CasePicDto :: getPicUrl).collect(Collectors.toList());

            List<String> detailPicsList = casePicList.stream()
                    .filter(doc->doc.getPicType() == 2).map(CasePicDto :: getPicUrl).collect(Collectors.toList());
            entity.setBannerPicsList(bannerPicsList);
            entity.setDetailPicsList(detailPicsList);
        }
        return entity;
    }

    @Override
    public void deleteCase(Long caseId) {
        caseMapper.deleteCase(caseId);
    }

    @Override
    public void addCase(CaseDto caseDto) {
        caseMapper.addCase(caseDto);
        saveCasePic(caseDto);
        // 添加商家案例
        caseMapper.addTenantCase(caseDto.getTenantId(), caseDto.getCaseId());
    }

    private void saveCasePic(CaseDto caseDto) {
        Long caseId = caseDto.getCaseId();
        List<CasePicDto> picList = Lists.newArrayList();
        List<String> bannerPicsList = caseDto.getBannerPicsList();
        for(String str : bannerPicsList){
            CasePicDto dto = new CasePicDto();
            dto.setPicUrl(str);
            dto.setPicType(1);
            picList.add(dto);
        }

        List<String> detailPicsList = caseDto.getDetailPicsList();
        for(String str : detailPicsList){
            CasePicDto dto = new CasePicDto();
            dto.setPicUrl(str);
            dto.setPicType(2);
            picList.add(dto);
        }
        if(CollectionUtils.isNotEmpty(picList)){
            caseMapper.addCasePic(caseId, picList);
        }
    }

    @Override
    public void updateCase(CaseDto caseDto) {
        caseMapper.updateCase(caseDto);
       // 删除casePic
        caseMapper.deleteCasePices(caseDto.getCaseId());
        // 新增casePic
        saveCasePic(caseDto);
    }

    @Override
    public List<caseStyleEntity> findCaseStyleEntity() {
        return caseMapper.findCaseStyleList();
    }

    @Override
    public List<caseStyleEntity> findCaseTypeList() {
        return caseMapper.findCaseTypeList();
    }
}
